//
//  YWInspectionMissionManager.h
//  maintain
//
//  Created by Kyo on 16/5/25.
//  Copyright © 2016年 ewin. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "YWSingleton.h"
#import "YWInspectionLineRecord.h"
#import "YWInspectionEquipmentRecord.h"
#import "YWInspectionMissionModel.h"
#import "YWMacro.h"

@class UIViewController;

typedef NS_ENUM(NSUInteger, YWInspectionEquipmentStatusType) {
    YWInspectionEquipmentStatusTypeUnfinish = 0, // 未巡
    YWInspectionEquipmentStatusTypeFinishNormal = 1, //正常
    YWInspectionEquipmentStatusTypeFinishTrouble = 2, //故障
};

typedef NS_ENUM(NSUInteger, YWInspectionJudgeType) {
    YWInspectionJudgeTypeMissionHasCancel = 0, //任务已取消
    YWInspectionJudgeTypeNotWeekDay = 1, //今天（星期*）不是巡查日
    YWInspectionJudgeTypeOutOfMissionTime = 2, //不在任务的执行时间内
    YWInspectionJudgeTypeMatch = 3, //匹配
};

typedef NS_ENUM(NSUInteger, YWInspectionEquipmentQrcodeResult) {
    YWInspectionEquipmentQrcodeResultUnMatch = 0, //不是巡更点
    YWInspectionEquipmentQrcodeResultHasPatrol = 1, //已经巡更过的点
    YWInspectionEquipmentQrcodeResultNoPatrol = 2, //未巡的点
};
static NSString *const KRefreshInspectionTimeLineNotification = @"KRefreshInspectionTimeLineNotification";
static NSString *const KRefreshInspectionLatestRecordNotification = @"KRefreshInspectionLatestRecordNotification";
static NSString *const KRefreshAssignInspectionMissionNotification = @"KRefreshAssignInspectionMissionNotification";
static NSString *const KRefreshDeleteInspectionMissionNotification = @"KRefreshDeleteInspectionMissionNotification";
static NSString *const KRefreshModifyInspectionMissionNotification = @"KRefreshModifyInspectionMissionNotification";

static NSString *const KRefreshCreateInspectionLineNotification = @"KRefreshCreateInspectionLineNotification";
static NSString *const KRefreshDeleteInspectionLineNotification = @"KRefreshDeleteInspectionLineNotification";
static NSString *const KRefreshModifyInspectionLineNotification = @"KRefreshModifyInspectionLineNotification";

@interface YWInspectionMissionManager : NSObject
YWSingletonH(Manager)

/**
 *  保存巡查任务
 *
 *  @param keepWatchMission 巡更任务任务模型数组
 *  @param needDelete 更新数据库是是否需要先删除所有任务再插入
 *
 */
- (BOOL)saveInspectionMissionWithArray:(NSArray*)inspectionMission;
/**
 *  所有巡查任务ID（逗号隔开）
 *
 *  @return 结果
 */
- (NSString*)allInspectionMissionIds;
/**
 *  更新巡查任务为删除状态
 *
 *  @return 结果
 */
- (BOOL)updateInspectionMissionDeleteStatusWithMissionId:(NSInteger)inspectionMissionId;
/**
 *  获取所有巡查任务
 *
 *  @param keepWatchMission 巡查任务模型数组
 */
- (NSArray *)allInspectionMissions;
/**
 *  按页数获取巡查任务
 *
 *  @param keepWatchMission 巡查任务模型数组
 */
- (NSArray *)inspectionMissionsWithPage:(NSInteger)page;
/**
 *  获取所有巡查路线
 *
 *  @param keepWatchMission 巡查路线模型数组
 */
- (NSArray *)allInspectionLines;
/**
 *  按类型分类的巡查任务
 *
 *  @param keepWatchMission 巡查任务模型数组
 */
- (NSArray *)twoTypeInspectionMissions;
/**
 *  根据任务id获取巡查任务
 *
 *  @param keepWatchMission 巡查任务模型数组
 */
- (YWInspectionMissionModel *)inspectionMissionWithMissionId:(NSInteger)missionId;
/**
 *  保存巡查任务线路记录
 *
 *  @param inspectionLineRecords 路线记录数组
 *
 *  @return 结果
 */
- (BOOL)updateInspectionLineRecordWithArray:(NSArray*)inspectionLineRecords;
///**
// *  更新从网络获取的巡查任务线路记录
// *
// *  @param inspectionLineRecords 路线记录数组
// *
// *  @return 结果
// */
//- (BOOL)updateInspectionLineRecordWithNewWorkData:(NSArray*)inspectionLineRecords;
/**
 *  保存巡查设备的记录
 *
 *  @param inspectionEquipmentRecords 设备记录数组
 *
 *  @return 结果
 */
- (BOOL)updateInspectionEquipmentRecordWithArray:(NSArray*)inspectionEquipmentRecords;
/**
 *  更新保存台账获取的位置巡查记录(不是全量字段更新)
 *
 *  @param locationInspectionRecords 位置巡查记录数组
 *
 *  @return 结果
 */
- (BOOL)updateLocationInspectionRecordWithArray:(NSArray*)locationInspectionRecords;
/**
 *  更新巡查设备的上传状态
 *
 *  @param uploadStatus 上传状态
 *
 *  @return 结果
 */
- (BOOL)updateInspectionEquipmentRecordUploadStatus:(YWInspectionEquipmentRecordUploadStatus)uploadStatus inspectionEquipmentRecordId:(NSString*)recordId;
/**
 *  按类型获取上传失败的巡查记录(二维数组)
 *
 *  @return 结果
 */
- (NSArray*)inspectionEquipmentRecordsWithUploadStatusFailWithType:(YWInspectionEquipmentRecordType)type;
/**
 *  获取上传失败的巡查记录
 *
 *  @return 结果
 */
- (NSArray*)uploadFailedInspectionRecords;

/**
 *  最新巡更路线记录
 *
 *  @param uniqueTag 完成状态
 *
 *  @return 结果
 */
- (YWInspectionLineRecord*)latestInspectionLineRecordUniqueTag:(NSString*)uniqueTag;
/**
 *  获取巡查路线记录
 *
 *  @param uniqueTag uniqueTag
 *
 *  @return 记录模型数组
 */
- (YWInspectionLineRecord*)inspectionLineRecordWithUniqueTag:(NSString*)uniqueTag;

/**
 *  巡查路线对象
 *
 *  @param patrolLineId 巡查路线id
 *
 *  @return 巡查路线对象
 */
- (YWInspectionLineModel *)inspectionLineWithLineId:(NSInteger)inspectionLineId;
/**
 *  获取巡查路线记录
 *
 *  @param inspectionMission 巡查任务
 *  @param page 页数
 *
 *  @return 记录模型数组
 */
- (NSArray*)inspectionLineRecordWithInspectionMission:(YWInspectionMissionModel*)inspectionMission page:(NSInteger)page;
/**
 *  获取巡查路线记录
 *
 *  @param page 页数
 *
 *  @return 记录模型数组
 */
- (NSArray*)inspectionLineRecordWithPage:(NSInteger)page;
/**
 *  更新记录已读状态
 *
 *  @return 无
 */
- (BOOL)updateReadStatusWithInspectionMissionId:(NSInteger)inspectionMissionId;
/**
 *  巡更点记录
 *
 *  @param uniqueTag 巡更路线记录标识
 *
 *  @return 记录模型数组
 */
- (NSArray*)inspectionEquipmentRecordWithUniqueTag:(NSString*)uniqueTag;
/**
 *  巡更点记录完成数量
 *
 *  @param uniqueTag 巡更路线记录标识
 *
 *  @return 记录模型数组
 */
- (NSInteger)countOfInspectionEquipmentRecordWithUniqueTag:(NSString*)uniqueTag;
/**
 *  所有巡查设备
 *
 *  @param inspectionLineId 路线id
 *
 *  @return 巡查equipment模型数组
 */
- (NSArray*)allInspectionEquipmentWithLineId:(NSInteger)inspectionLineId;
/**
 *  所有巡查点
 *
 *  @param inspectionLineId 巡查路线id
 *
 *  @return 巡查location模型数组
 */
- (NSArray*)allInspectionLocationWithInspectionLineId:(NSInteger)inspectionLineId;
/**
 *  获取巡查点备注
 *
 *  @param locationId 位置
 *
 *  @return 巡查location模型数组
 */
- (NSArray*)inspectionLocationNoteWithLocationId:(NSInteger)locationId lineIds:(NSArray*)inspectionLineIdArray;
/**
 *  二维码下所有巡查的设备
 *
 *  @param inspectionLineId 路线id
 *
 *  @return 设备id数组
 */
- (NSArray*)allInspectionEquipmentWithLineId:(NSInteger)inspectionLineId qrcodeId:(NSString*)qrcodeId orLocationId:(NSInteger)locationId;
/**
 *  二维码下某种类型的要巡查设备
 *
 *  @param inspectionLineId 路线id
 *
 *  @return 设备id数组
 */
- (NSArray*)allInspectionEquipmentWithLineId:(NSInteger)inspectionLineId qrcodeId:(NSString*)qrcodeId equipmentTypeId:(NSInteger)equipmentTypeId;
/**
 *  获取巡查线路数据
 *
 *  @param inspectionLine 巡查路线对象
 *
 *  @return 结果
 */
- (NSArray*)inspectionLineDataWithInspectionLine:(YWInspectionLineModel*)inspectionLine;
/**
 *  获取线路view的数据
 *
 *  @param inspectionLineId 巡查路线id
 *  @param inspectionMissionId 巡查任务id
 *
 *  @return 结果
 */
- (NSArray*)timeLineViewDataWithInspectionMission:(YWInspectionMissionModel*)inspectionMission orLineRecord:(YWInspectionLineRecord*)inspectionLineRecord;
//巡查设备
- (NSArray*)timeLineViewDataWithLocationMission:(YWInspectionMissionModel*)inspectionMission orLineRecord:(YWInspectionLineRecord*)inspectionLineRecord;

/**
 *  记录详情页的线路view的数据
 *
 *  @param lineRecord 巡查路线记录
 *
 *  @return 结果
 */
- (NSArray*)timeLineViewDataWithInspectionRecord:(YWInspectionLineRecord *)record;
//巡查位置
- (NSArray*)timeLineViewDataWithLocationRecord:(YWInspectionLineRecord *)record;

//巡查任务扫描的判断
- (YWInspectionJudgeType)judgeTypeWithMission:(YWInspectionMissionModel*)inspectionMission;
- (NSString*)typeNameWithJudgeType:(YWInspectionJudgeType)judgeType;

/**
 *  判断扫描的巡查设备的状态
 *
 *  @param inspectionEquipments 要巡查的设备
 *  @param equipmentRecord 巡查记录
 *  @param qrcodeEquipments 二维码中要巡查设备
 *
 *  @return 结果
 */
- (YWInspectionEquipmentQrcodeResult)judgeInspectionWithInspectionEquipment:(NSArray* )inspectionEquipments equipmentRecord:(NSArray*)equipmentRecord qrcodeEquipment:(NSArray*)qrcodeEquipments;
//巡查位置的判断
- (YWInspectionEquipmentQrcodeResult)judgeInspectionPointWithLocation:(NSArray* )inspectionLocation pointRecord:(NSArray*)pointRecord locationId:(NSInteger)locationId;
/**
 *  新建巡查线路纪录
 *
 *  @return 结果
 */
- (YWInspectionLineRecord *)createLooperRecordWith:(YWInspectionMissionModel*)inspectionMission uniqueTag:(NSString*)uniqueTag;
/**
 *  做设备巡查(扫码成功的回调方法)
 *
 *  @return 结果
 */
- (void)qrcodeSuccessWithEquipmentInspection:(YWInspectionMissionModel*)inspectionMission qrcodeId:(NSString*)qrcodeId noQrcodeLocationId:(NSInteger)noQrcodeLocationId uniqueTag:(NSString*)uniqueTag wReader:(UIViewController*)wReader isFromLedger:(BOOL)isFromLedger;
/**
 *  做位置巡查(扫码成功的回调方法)
 *
 *  @return 结果
 */
- (void)qrcodeSuccessWithLocationInspection:(YWInspectionMissionModel*)inspectionMission qrcodeId:(NSString*)qrcodeId noQrcodeLocationId:(NSInteger)noQrcodeLocationId uniqueTag:(NSString*)uniqueTag wReader:(UIViewController*)wReader isFromLedger:(BOOL)isFromLedger;

/**
 *  请求楼盘下所有的巡查线路
 *
 *  @return 结果
 */
- (void)requestInspectionLineWithBuildingId:(NSString *)buildingId Complete:(void(^)(NSArray *lineOptionArray))complete;
/**
 *  请求巡查线路的详情
 *
 *  @return 结果
 */
- (void)requestInspectionLineWithInspectionLineId:(NSInteger)lineId Complete:(void(^)(YWInspectionLineModel *inspectionLine))complete;

/**
 *  分配巡查任务
 *
 *  @return 结果
 */
- (void)postInspectionMissionWithParams:(NSDictionary*)params Complete:(void(^)(BOOL success))complete;
/**
 *  请求删除巡查任务
 *
 *  @param missionId 任务id
 *
 *  @return 无
 */
- (void)requestDeleteInspectionMission:(NSInteger)inspectionMissionId completeBlock:(void (^)(BOOL))complete;
/**
 *  请求修改巡查任务
 *
 *  @param params 参数
 *
 *  @return 无
 */
- (void)requestModifyInspectionMission:(NSDictionary*)params completeBlock:(void (^)(BOOL))complete;
/**
 *  创建巡查路线
 *
 *  @param params 参数
 *
 *  @return 无
 */
- (void)createInspectionLine:(NSDictionary*)params completeBlock:(void (^)(NSInteger))complete;
/**
 *  删除巡查路线
 *
 *  @param params 参数
 *
 *  @return 无
 */
- (void)deleteInspectionLineWithLineId:(NSInteger)inspectionLineId completeBlock:(void (^)(NSInteger))complete;
/**
 *  修改巡查路线
 *
 *  @param params 参数
 *
 *  @return 无
 */
- (void)modifyInspectionLine:(NSDictionary*)params completeBlock:(void (^)(BOOL))complete;
/**
 *  请求巡查任务
 *
 *  @return 结果
 */
- (void)requestInspectionMissionWithPage:(NSInteger)page pageSize:(NSInteger)pageSize complete:(void(^)(BOOL success))complete;
/**
 *  请求单条巡查任务
 *
 *  @return 结果
 */
- (void)requestInspectionMissionWithMissionId:(NSInteger)missionId Complete:(void(^)(YWInspectionMissionModel* inspectionMission))complete;

/**
 *  请求与我相关的巡查线路记录
 *
 *  @return 结果
 */
- (void)requestInspectionLineRecordWithPage:(NSInteger)page pageSize:(NSInteger)pageSize complete:(void(^)(BOOL success))complete;
/**
 *  请求巡查线路记录
 *
 *  @return 结果
 */
- (void)requestInspectionLineRecordWithPage:(NSInteger)page pageSize:(NSInteger)pageSize inspectionMissionId:(NSInteger)inspectionMissionId inspectionLineId:(NSInteger)inspectionLineId WithComplete:(void(^)(BOOL success))complete;
/**
 *  请求一条巡查记录详情
 *
 *  @return 结果
 */
- (void)requestInspectionEquipmentRecord:(NSString*)uniqueTag type:(YWInspectionEquipmentRecordType)type WithComplete:(void(^)(BOOL success))complete;
/**
 *  请求某个位置的巡查记录
 *
 *  @return 结果
 */
- (void)requestInspectionLocationRecord:(NSInteger)locationId page:(NSInteger)page pageSize:(NSInteger)pageSize complete:(void(^)(BOOL success))complete;
/**
 *  请求巡查任务状态
 *
 *  @return 结果
 */
- (void)requestInspectionMissionStatusWithIds:(NSString *)ids Complete:(void(^)(BOOL success))complete;
/**
 *  开始后台上传任务
 *  @param records        记录模型数组
 *  @param pictures       记录中所有的图片数组
 */
- (void)startUploadInspectionRecords:(NSArray*)records pictures:(NSArray*)pictures WithType:(YWInspectionEquipmentRecordType)type;
/**
 *  上传失败的巡更点记录
 *
 *  @return 记录点模型数组
 */
- (void)uploadFailInspectionRecords;

/**
 *  设备是否有巡查记录
 *
 *  @param uniqueTag 巡查路线记录标识
 *
 *  @return 结果
 */
- (BOOL)isExistRecordWithUniqueTag:(NSString *)uniqueTag equipmentId:(NSString*)equipmentId;
/**
 *  线路是否有设备
 *
 *  @param inspectionLineId 巡查路线Id
 *
 *  @return 结果
 */
- (BOOL)isExistEquipmentWithLineId:(NSInteger)inspectionLineId;
/**
 *  线路是否有位置
 *
 *  @param inspectionLineId 巡查路线Id
 *
 *  @return 结果
 */
- (BOOL)isExistLocationWithLineId:(NSInteger)inspectionLineId;

/**
 *  获取扫码匹配的巡查任务
 *
 *  @param qrcodeId 二维码Id
 *
 *  @return 结果
 */
-(NSArray*)matchInspectionMissionsWithQrcodeID:(NSString*)qrcodeId orLocationId:(NSInteger)locationId;
-(NSArray*)matchInspectionMissionsWithQrcodeID:(NSString*)qrcodeId orLocationId:(NSInteger)locationId inspectionLineType:(YWInspectionLineType)lineType equipmentId:(NSString*)equipmentId;

/**
 *  不完整的，需要请求的路线数组
 *
 *  @return 只有id和updateTime的路线对象数组
 */
- (NSArray*)needRequestInspectionlLines;
/**
 *  请求任务的latestRecord
 *
 *  @return 结果
 */
- (void)requestInspectionMissionInfo:(NSInteger)inspectionMissionId complete:(void (^)(BOOL))complete;
/**
 *  批量请求巡查路线
 *
 *  @return 结果
 */
- (void)requestInspectionLineWithLineArray:(NSArray *)inspectionLineArray complete:(void (^)(BOOL))complete;
/**
 *  请求设备的巡查记录
 *
 *  @return 结果
 */
- (void)requestInspectionEquipmentRecordWithEquipmentIds:(NSString*)equipmentIds page:(NSInteger)page pageSize:(NSInteger)pageSize completeBlock:(void (^)(NSInteger))complete;
@end



